<?php

namespace App\Models;

use CodeIgniter\Model;

class NewsModel extends Model
{
    protected $table = 'news';
    protected $primaryKey = 'id';
    protected $allowedFields = ['title', 'slug', 'body'];
    protected $returnType = 'object';

    public function getNews($slug = false)
    {
        if ($slug === false) {
            // 如果没有提供slug，则获取所有新闻
            return $this->findAll();
        }

        // 根据slug获取一条新闻
        return $this->where(['slug' => $slug])->first();
    }
}
